在 静态类型语言中,不可变性是数据的基础状态,确保内存安全并实现可预测的执行。虽然变量可以被遮蔽或设为可变,但 常量 则更进一步地将一个值永久绑定到名称上。
1. 常量的严格性
与标准变量不同,标准变量通过 类型推断 让编译器推导出数据类型,而常量则必须明确声明 显式类型注解 (例如, : u32)。这确保了代码二进制文件内部具有严格的契约。
2. 编译时求值
常量不仅仅是不可变变量;它们在编译时就被计算并“固化”进程序的二进制文件中。这使得编译器能够在程序运行前就执行 常量表达式 (如 60 * 60 * 3)),从而优化性能。
3. 绝对不可变性
常量作为“唯一真实来源”。它们不能使用 mut 关键字变为可变,也不能在同一作用域内被遮蔽,从而确保程序的关键参数在整个执行过程中保持不变。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following is REQUIRED when declaring a constant in Rust?
The 'mut' keyword
A type annotation (e.g., : u32)
A semicolon before the name
The 'let' keyword
✅ Correct!
Rust constants must always have their type explicitly stated; the compiler will not use type inference for them.❌ Incorrect
Unlike 'let' variables, constants demand explicit type annotations to satisfy the language's safety contracts.QUESTION 2
When are the values of constants calculated?
At runtime, when the variable is first accessed.
During program initialization only.
At compile-time.
Only when the user provides input.
✅ Correct!
Constants are evaluated at compile-time, allowing the compiler to optimize the program binary with the result.❌ Incorrect
Calculations for constants happen before the program ever runs, which is why they are limited to constant expressions.QUESTION 3
Can a constant be shadowed in the same scope?
Yes, using the 'let' keyword.
Yes, if the value is the same type.
No, constants cannot be shadowed in the same scope.
Only within a function body.
✅ Correct!
Constants are absolutely immutable and cannot be shadowed in the same scope, serving as a permanent source of truth.❌ Incorrect
Shadowing applies to variables declared with 'let', not to 'const' definitions.QUESTION 4
What is a 'Constant Expression'?
An expression that returns a random number.
An expression that can be computed at compile-time.
A string literal that cannot be formatted.
A mathematical formula used only in 'mut' variables.
✅ Correct!
Constant expressions are logic or math (like 60 * 60) that the compiler resolves during the build process.❌ Incorrect
Constant expressions must be deterministic and resolvable by the compiler without running the program.QUESTION 5
Which keyword is used to define a constant in Rust?
let immutable
define
const
static_val
✅ Correct!
The 'const' keyword is specifically used for these permanent, compile-time values.❌ Incorrect
In Rust, 'const' is the reserved keyword for absolute constants.Module: Security & Performance via Constants
Optimizing a Web Server's Configuration
You are developing a high-performance web server. You need to define a session timeout of 3 hours that is used across multiple modules. To prevent any logic from accidentally changing this duration and to ensure maximum runtime speed, you decide to use a constant.
Q
1. Write the Rust code to define 'THREE_HOURS_IN_SECONDS' as a u32 constant.
Solution:
Note the mandatory type and the use of math that the compiler will solve.
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;Note the mandatory type and the use of math that the compiler will solve.
Q
2. Why is this more performant than using a standard variable?
Solution:
Because the value is computed at compile-time. The program doesn't waste CPU cycles at runtime doing the multiplication; it simply loads the final result (10800) directly from the binary.
Because the value is computed at compile-time. The program doesn't waste CPU cycles at runtime doing the multiplication; it simply loads the final result (10800) directly from the binary.
Q
3. If a developer tries to use shadowing or the 'mut' keyword on this constant, what happens?
Solution:
The compiler will throw an error. Constants do not support 'mut', and shadowing rules for variables do not apply to constants in the same way, preventing accidental state corruption.
The compiler will throw an error. Constants do not support 'mut', and shadowing rules for variables do not apply to constants in the same way, preventing accidental state corruption.